﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EcSalseManage
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Session.Abandon();
                //this.ibtnReset.Attributes.Add("onclick", "Form1.reset();return false");
                this.ibtnLogin.Attributes.Add("onclick", "return UserCheck();");

                if (Request.Cookies["UserName"] != null&&Request.Cookies["UserPwd"]!=null) // 从客户端读取cookie值
                {
                    if (Login(Request.Cookies["UserName"].Value, Request.Cookies["UserPwd"].Value))
                    {
                       this.Response.Redirect("Default.aspx");
                    }
                }
            }
            this.Page.SetFocus(this.txtName);
        }

        #region 身份验证
        protected void ibtnLogin_Click(object sender, EventArgs e)
        {
            if (Login(this.txtName.Text, this.txtPassword.Text))
            {
                if (ckbRemenber.Checked == true)
                {
                    Response.Cookies["UserName"].Value = this.txtName.Text;  //将值写入到客户端硬盘Cookie
                    Response.Cookies["UserPwd"].Value = this.txtPassword.Text;
                    Response.Cookies["UserName"].Expires = DateTime.Now.AddMinutes(7200);//设置Cookie过期时间
                    Response.Cookies["UserPwd"].Expires = DateTime.Now.AddMinutes(7200);//设置Cookie过期时间
                }
                this.Response.Redirect("Default.aspx");
            }

        }
        #endregion


        private bool Login(string name, string pwd)
        {
            DataAccess.Operate.DA_Login_Inherit Login_Inherit = new DataAccess.Operate.DA_Login_Inherit();
            bool bLogin_IsBusiness;
            int iLogin_ID;
            string sPurview = "";
            string sBuilding = "";
            DataTable dt;
            bool toreturn = true;
            try
            {
                if (Login_Inherit.Login_Check(name, pwd, out bLogin_IsBusiness, out iLogin_ID) == true)
                {
                    DataAccess.KDHR.Service wsKDHR = new DataAccess.KDHR.Service();
                    DataSet ds = new DataSet();

                    ds = wsKDHR.FindEmployeesByEmpCodeOnly(name);
                    Session["EmployeeID"] = name;
                    Session["EmployeeName"] = ds.Tables[0].Rows[0]["EmpName"].ToString();
                    Session["IsBusiness"] = bLogin_IsBusiness;
                    Session["Login_ID"] = iLogin_ID;
                    dt = Login_Inherit.getUserPurview(iLogin_ID);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            sPurview = sPurview + dt.Rows[i]["PermissionForECSM_Module_Purview"].ToString();
                        }
                    }
                    Session["Purview"] = sPurview;
                    if (Session["IsBusiness"].ToString() == "True")
                    {
                        dt = Login_Inherit.getUserBuilding(iLogin_ID);
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i <= dt.Rows.Count - 1; i++)
                            {
                                sBuilding += "'" + dt.Rows[i]["User_BuildingForECSM_BuildingID"].ToString() + "',";
                            }
                            sBuilding = sBuilding.Substring(0, sBuilding.Length - 1);
                        }
                        Session["Building"] = sBuilding;
                    }
                }
                else
                {
                    toreturn = false;
                    this.Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script language=javascript>alert('" + Common.Common_Const.MSG_LOGIN + "');</script>");
                }
                return toreturn;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                Login_Inherit = null;
            }
        }
    }
}
